


if (!config.runsInWidget) {
    a=new Alert()
    a.title="running on non iphone desktop"
    a.presentAlert()
}

let widget= new ListWidget()

const wechatDay='2019-09-07'
const handsOnDay='2019-10-12'
let now = today()
let title = widget.addText("那一天阳光正好☀️，我们相遇了。")
title.font = Font.lightSystemFont(20)
title.centerAlignText()
title.textColor = Color.white()

let line = widget.addText("————".repeat(4))
line.textOpacity = 0.5
line.centerAlignText()
line.textColor = Color.white()

widget.addSpacer(20)

let firstPara=widget.addText("📱  与你相识的第"+daysDistance(now,wechatDay).toString()+"天。")
firstPara.font = Font.lightSystemFont(22)
firstPara.textColor = Color.white()
widget.addSpacer(30)
let secondPara=widget.addText("💗  与你共老的第"+daysDistance(now,handsOnDay).toString()+"天。")
secondPara.font = Font.lightSystemFont(22)
secondPara.textColor = Color.white()

let background= await getImage("http://image.wufazhuce.com/FrM45oLlRl8aW95py8ecF0SSrKl9")
widget.backgroundImage = await shadowImage(background)

Script.setWidget(widget)
Script.complete()

async function getImage(url){
    let request = new Request(url)
    return await request.loadImage()
}


async function shadowImage (img) {
    let ctx = new DrawContext()
    ctx.size = img.size
    ctx.drawImageInRect(img, new Rect(0, 0, img.size['width'], img.size['height']))
    ctx.setFillColor(new Color('#000000', 0.5))
    ctx.fillRect(new Rect(0, 0, img.size['width'], img.size['height']))
    return await ctx.getImage()
}

function today(){
    let today=new Date();
    let str="";
    str+=today.getFullYear()+"-";
    let month=today.getMonth()+1;
    if(month<10){
        str+="0";
    }
    str+=month+"-";
    let day=today.getDate();
    if(day<10){
        str+="0";
    }
    str+=day;
    return str;
}

function daysDistance(date1, date2) {
    date1 = Date.parse(date1);
    date2 = Date.parse(date2);
    let distance= Math.abs(date2 - date1);
    let days = Math.floor(distance / (24 * 3600 * 1000));
    return days ;
}